import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-15
 * Time: 11:58
 */
public class JDBCDemo3 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入需要查询的id号");
        int id= scanner.nextInt();
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/d09073?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1111");

        Connection connection=dataSource.getConnection();

        String sql="select * from student where id = ?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,id);

        // 执行查询操作, 要使用 executeQuery. 返回值是一个 ResultSet 类型的对象. 表示了一个 "表格"
        ResultSet resultSet=statement.executeQuery();

        while(resultSet.next()){
            int id1= resultSet.getInt("id");
            String name= resultSet.getString("name");
            int id2= resultSet.getInt("classId");
            System.out.println("id="+id1+" name="+name+" classId="+id2);
        }

        resultSet.close();
        statement.close();
        connection.close();

    }
}
